home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 19 / Amiga Plus Leser CD 19.iso / Tools / MorphOS / cvs-1.11.2 / source / amiga / amiga.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-11-18  |  61.1 KB  |  3,131 lines

  1. /*
  2.  * $Id$
  3.  *
  4.  * :ts=4
  5.  *
  6.  * AmigaOS wrapper routines for GNU CVS, using the RoadShow TCP/IP API
  7.  *
  8.  * Written and adapted by Olaf `Olsen' Barthel <olsen@sourcery.han.de>
  9.  *                        Jens Langner <Jens.Langner@light-speed.de>
  10.  *
  11.  * This program is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  */
  25.  
  26. #include "SDI_compiler.h"
  27.  
  28. #include <exec/memory.h>
  29.  
  30. #include <pwd.h>
  31. #include <grp.h>
  32.  
  33. #include <proto/bsdsocket.h>
  34. #include <proto/dos.h>
  35. #include <proto/exec.h>
  36. #include <proto/locale.h>
  37. #include <proto/utility.h>
  38.  
  39. #include <dos/dostags.h>
  40.  
  41. #include <libraries/locale.h>
  42. #include <workbench/startup.h>
  43.  
  44. #include <sys/types.h>
  45. #include <sys/socket.h>
  46. #include <sys/stat.h>
  47. #include <sys/ioctl.h>
  48.  
  49. /* includes only for SASC or which SASC don`t like */
  50. #if defined(__SASC)
  51.   #include <ios1.h>
  52. #else
  53.   #include <sys/fcntl.h>
  54. #endif
  55.  
  56. #include <utime.h>
  57. #include <stdio.h>
  58. #include <errno.h>
  59. #include <dirent.h>
  60. #include <netdb.h>
  61. #include <signal.h>
  62. #include <stdlib.h>
  63. #include <time.h>
  64. #include <string.h>
  65. #include <unistd.h>
  66. #include <stdarg.h>
  67.  
  68. /****************************************************************************/
  69.  
  70. //#define DEBUG
  71. #include "_assert.h"
  72.  
  73. /****************************************************************************/
  74.  
  75. #define const
  76. #define NO_NAME_REPLACEMENT
  77. #include "amiga.h"
  78. #undef const
  79.  
  80. #include "error.h"
  81.  
  82. /****************************************************************************/
  83.  
  84. #include "ssh_protocol.h"
  85.  
  86. /****************************************************************************/
  87.  
  88. #define UNIX_TIME_OFFSET 252460800
  89.  
  90. /****************************************************************************/
  91.  
  92. #define ZERO    ((BPTR)NULL)
  93. #define SAME    (0)
  94. #define OK        (0)
  95. #define CANNOT    !
  96. #define NOT        !
  97.  
  98. /****************************************************************************/
  99.  
  100. /* This macro lets us long-align structures on the stack */
  101. #define D_S(type,name) \
  102.     char a_##name[sizeof(type)+3]; \
  103.     type *name = (type *)((LONG)(a_##name+3) & ~3)
  104.  
  105. /****************************************************************************/
  106.  
  107. #define NUM_ENTRIES(t)        (sizeof(t) / sizeof(t[0]))
  108.  
  109. /****************************************************************************/
  110.  
  111. #define FIB_IS_FILE(fib)    ((fib)->fib_DirEntryType < 0)
  112. #define FIB_IS_DRAWER(fib)    ((fib)->fib_DirEntryType >= 0 && \
  113.                              (fib)->fib_DirEntryType != ST_SOFTLINK && \
  114.                              (fib)->fib_DirEntryType != ST_LINKDIR)
  115.  
  116. /****************************************************************************/
  117.  
  118. #define FLAG_IS_SET(v,f)      (((v) & (f)) == (f))
  119. #define FLAG_IS_CLEAR(v,f)    (((v) & (f)) == 0)
  120. #define SET_FLAG(v,f)            ((v) |= (f))
  121.  
  122. /****************************************************************************/
  123.  
  124. extern void        error(int,int,const char *,...);
  125. extern void *    xmalloc(size_t size);
  126. extern char *    xstrdup(const char * const str);
  127. extern char *    scramble(char *str);
  128. extern char *    descramble(char *str);
  129. extern char *    get_homedir(void);
  130. extern int        getline(char **lineptr,size_t *n,FILE *stream);
  131.  
  132. /****************************************************************************/
  133.  
  134. struct Library * SocketBase;
  135.  
  136. /****************************************************************************/
  137.  
  138. #if defined(__SASC) || defined(__GNUC__)
  139.   /* GCC (libnix) supports the same as SAS/C! */
  140.   long __stack = 20000;
  141.   long __buffsize = 8192;
  142.   long _MSTEP = 16384;
  143. #endif
  144.  
  145. /****************************************************************************/
  146.  
  147. /* add some features GNUC doesn`t provide by default
  148.    and take care that the libnix of morphOS supports some of those per default
  149. */
  150. #if defined(__GNUC__)
  151.   #if !defined(__MORPHOS__)
  152.     STRPTR _ProgramName;
  153.   #endif
  154.  
  155.   /* libnix catches the WB startup code for us, so we don`t need to worry */
  156.   extern struct WBStartup *_WBenchMsg;
  157.   #define WBenchMsg _WBenchMsg
  158.  
  159.   /* prototype for the chkabort() function (libnix supports it) */
  160.   #define chkabort() __chkabort()
  161.   extern void chkabort();
  162. #endif
  163.  
  164. /****************************************************************************/
  165.  
  166. #if defined(__PPC__)
  167.   #if defined(__MORPHOS__)
  168.     #define CPU " [MOS/PPC]"
  169.   #else
  170.     #define CPU " [OS4/PPC]"
  171.   #endif
  172. #elif defined(_M68060) || defined(__M68060) || defined(__mc68060)
  173.     #define CPU " [060]"
  174. #elif defined(_M68040) || defined(__M68040) || defined(__mc68040)
  175.     #define CPU " [040]"
  176. #elif defined(_M68030) || defined(__M68030) || defined(__mc68030)
  177.     #define CPU " [030]"
  178. #elif defined(_M68020) || defined(__M68020) || defined(__mc68020)
  179.     #define CPU " [020]"
  180. #else
  181.     #define CPU ""
  182. #endif
  183.  
  184. /****************************************************************************/
  185.  
  186. const char VersTag[] = "$VER: cvs 1.11.2" CPU " (09.11.2002) ported by Olaf Barthel and Jens Langner";
  187.  
  188. /****************************************************************************/
  189.  
  190. static void map_ioerr_to_errno(void);
  191. static void get_next_buffer(char **buffer_ptr);
  192. static void correct_name(char **name_ptr);
  193. static void close_libs(void);
  194. static void initialize_libraries(void);
  195. static int recursive_unlink_file_dir(char *f);
  196. static int compare(char **a,char **b);
  197. static void convert_fileinfo_to_stat(struct FileInfoBlock *fib,struct stat *st);
  198. static void restore_home_dir(void);
  199. static int amiga_rcmd(char **remote_hostname,int remote_port,char *local_user,char *remote_user,char *command);
  200.  
  201. /****************************************************************************/
  202.  
  203. static void map_ioerr_to_errno(void)
  204. {
  205.     /* This routine maps AmigaDOS error codes to
  206.      * Unix error codes, as far as this is possible.
  207.      * This table contains AmigaDOS error codes
  208.      * the emulated routines won't generate. I have
  209.      * included them for the sake of completeness.
  210.      */
  211.     struct { LONG IoErr; int errno; } map_table[] =
  212.     {
  213.         { ERROR_NO_FREE_STORE,                    ENOMEM        },
  214.         { ERROR_TASK_TABLE_FULL,                  ENOMEM        },
  215.         { ERROR_BAD_TEMPLATE,                        EINVAL        },
  216.         { ERROR_BAD_NUMBER,                          EINVAL        },
  217.         { ERROR_REQUIRED_ARG_MISSING,            EINVAL        },
  218.         { ERROR_KEY_NEEDS_ARG,                    EINVAL        },
  219.         { ERROR_TOO_MANY_ARGS,                    EINVAL        },
  220.         { ERROR_UNMATCHED_QUOTES,                  EINVAL        },
  221.         { ERROR_LINE_TOO_LONG,                    ENAMETOOLONG  },
  222.         { ERROR_FILE_NOT_OBJECT,                  ENOEXEC       },
  223.         { ERROR_INVALID_RESIDENT_LIBRARY,      EIO           },
  224.         { ERROR_NO_DEFAULT_DIR,                    EIO           },
  225.         { ERROR_OBJECT_IN_USE,                    EBUSY         },
  226.         { ERROR_OBJECT_EXISTS,                    EEXIST        },
  227.         { ERROR_DIR_NOT_FOUND,                    ENOENT        },
  228.         { ERROR_OBJECT_NOT_FOUND,                  ENOENT        },
  229.         { ERROR_BAD_STREAM_NAME,                  EINVAL        },
  230.         { ERROR_OBJECT_TOO_LARGE,                  EFBIG         },
  231.         { ERROR_ACTION_NOT_KNOWN,                  ENOSYS        },
  232.         { ERROR_INVALID_COMPONENT_NAME,        EINVAL        },
  233.         { ERROR_INVALID_LOCK,                        EBADF         },
  234.         { ERROR_OBJECT_WRONG_TYPE,              EFTYPE        },
  235.         { ERROR_DISK_NOT_VALIDATED,              EROFS         },
  236.         { ERROR_DISK_WRITE_PROTECTED,            EROFS         },
  237.         { ERROR_RENAME_ACROSS_DEVICES,        EXDEV         },
  238.         { ERROR_DIRECTORY_NOT_EMPTY,            ENOTEMPTY     },
  239.         { ERROR_TOO_MANY_LEVELS,                  ENAMETOOLONG  },
  240.         { ERROR_DEVICE_NOT_MOUNTED,            ENXIO         },
  241.         { ERROR_SEEK_ERROR,                          EIO           },
  242.         { ERROR_COMMENT_TOO_BIG,                ENAMETOOLONG  },
  243.         { ERROR_DISK_FULL,                          ENOSPC        },
  244.         { ERROR_DELETE_PROTECTED,                EACCES        },
  245.         { ERROR_WRITE_PROTECTED,                EACCES        },
  246.         { ERROR_READ_PROTECTED,                    EACCES        },
  247.         { ERROR_NOT_A_DOS_DISK,                    EFTYPE        },
  248.         { ERROR_NO_DISK,                              EACCES        },
  249.         { ERROR_NO_MORE_ENTRIES,                EIO           },
  250.         { ERROR_IS_SOFT_LINK,                        EFTYPE        },
  251.         { ERROR_OBJECT_LINKED,                    EIO           },
  252.         { ERROR_BAD_HUNK,                   ENOEXEC       },
  253.         { ERROR_NOT_IMPLEMENTED,                ENOSYS        },
  254.         { ERROR_RECORD_NOT_LOCKED,            EIO           },
  255.         { ERROR_LOCK_COLLISION,                    EACCES        },
  256.         { ERROR_LOCK_TIMEOUT,                        EIO           },
  257.         { ERROR_UNLOCK_ERROR,               EIO           },
  258.         { ERROR_BUFFER_OVERFLOW,            EIO           },
  259.         { ERROR_BREAK,                                EINTR         },
  260.         { ERROR_NOT_EXECUTABLE,                    ENOEXEC       }
  261.     };
  262.  
  263.     LONG Error = IoErr();
  264.  
  265.     if(Error != OK)
  266.     {
  267.         int i;
  268.  
  269.         /* If nothing else matches, we can always
  270.          * flag it as an I/O error.
  271.          */
  272.         errno = EIO;
  273.  
  274.         for(i = 0 ; i < NUM_ENTRIES(map_table) ; i++)
  275.         {
  276.             if(map_table[i].IoErr == Error)
  277.             {
  278.                 errno = map_table[i].errno;
  279.                 break;
  280.             }
  281.         }
  282.     }
  283. }
  284.  
  285. /****************************************************************************/
  286.  
  287. int REGARGS amiga_get_minutes_west(void)
  288. {
  289.     int minutes_west;
  290.  
  291.     if(LocaleBase == NULL)
  292.   {
  293.     #if defined(__MORPHOS__)
  294.           LocaleBase = OpenLibrary("locale.library", 38);
  295.     #else
  296.       LocaleBase = (struct LocaleBase *)OpenLibrary("locale.library", 38);
  297.     #endif
  298.  
  299.         if(LocaleBase != NULL)
  300.             atexit(close_libs);
  301.     }
  302.  
  303.  
  304.     if(LocaleBase != NULL)
  305.     {
  306.         struct Locale * loc;
  307.  
  308.         loc = OpenLocale(NULL);
  309.  
  310.         minutes_west = loc->loc_GMTOffset;
  311.  
  312.         CloseLocale(loc);
  313.     }
  314.     else
  315.     {
  316.         minutes_west = 0;
  317.     }
  318.  
  319.     return(minutes_west);
  320. }
  321.  
  322. /****************************************************************************/
  323.  
  324. #define MAX_FILENAME_LEN 1024
  325.  
  326. /****************************************************************************/
  327.  
  328. static void get_next_buffer(char ** buffer_ptr)
  329. {
  330.     static char buffer_slots[8][MAX_FILENAME_LEN];
  331.     static int buffer_index;
  332.  
  333.     (*buffer_ptr)    = buffer_slots[buffer_index];
  334.     buffer_index    = (buffer_index + 1) % 8;
  335. }
  336.  
  337. /****************************************************************************/
  338.  
  339. static void correct_name(char ** name_ptr)
  340. {
  341.     char * buffer;
  342.     int len,i;
  343.     char * name;
  344.  
  345.     ENTER();
  346.  
  347.     name = (*name_ptr);
  348.  
  349.     if(name[0] == '/')
  350.     {
  351.         BOOL done;
  352.  
  353.         SHOWSTRING(name);
  354.  
  355.         get_next_buffer(&buffer);
  356.  
  357.         done = FALSE;
  358.  
  359.         len = strlen(name);
  360.         for(i = 1 ; i <= len ; i++)
  361.         {
  362.             if(name[i] == '/' || name[i] == '\0')
  363.             {
  364.                 memcpy(buffer, name+1, i);
  365.                 buffer[i-1] = ':';
  366.  
  367.                 if(name[i] != '\0') strcpy(&buffer[i],&name[i+1]);
  368.         else buffer[i] = '\0';
  369.  
  370.                 done = TRUE;
  371.                 break;
  372.             }
  373.         }
  374.  
  375.         if(NOT done)
  376.             strcpy(buffer,name);
  377.  
  378.         SHOWSTRING(buffer);
  379.  
  380.         name = buffer;
  381.     }
  382.     else
  383.     {
  384.         len = strlen(name);
  385.  
  386.         SHOWSTRING(name);
  387.  
  388.         for(i = 0 ; i < len-1 ; i++)
  389.         {
  390.             if(name[i] == ':' && name[i+1] == '/')
  391.             {
  392.                 get_next_buffer(&buffer);
  393.  
  394.                 memcpy(buffer,name,i+1);
  395.                 strcpy(&buffer[i+1],&name[i+2]);
  396.  
  397.                 SHOWSTRING(buffer);
  398.  
  399.                 name = buffer;
  400.                 break;
  401.             }
  402.         }
  403.     }
  404.  
  405.     if(strncmp(name,"./",2) == SAME)
  406.     {
  407.         get_next_buffer(&buffer);
  408.  
  409.         strcpy(buffer,name+2);
  410.         name = buffer;
  411.     }
  412.     else if (strncmp(name,"../",3) == SAME)
  413.     {
  414.         get_next_buffer(&buffer);
  415.  
  416.         strcpy(buffer,name+3);
  417.         name = buffer;
  418.     }
  419.     else if (strcmp(name,".") == SAME)
  420.     {
  421.         get_next_buffer(&buffer);
  422.  
  423.         strcpy(buffer,"");
  424.         name = buffer;
  425.     }
  426.     else if (strcmp(name,"..") == SAME)
  427.     {
  428.         get_next_buffer(&buffer);
  429.  
  430.         strcpy(buffer,"/");
  431.         name = buffer;
  432.     }
  433.  
  434.     len = strlen(name);
  435.     if(len > 1)
  436.   {
  437.     if(name[len-1] == '/') name[--len] = '\0';
  438.     if(name[len-1] == '.' && name[len-2] == '/') name[--len] = '\0';
  439.   }
  440.  
  441.     /* The following has been commented out since it is not for
  442.      * everyday use. Some CVS servers hosted on systems that default
  443.      * to the PC850 code page for international characters will return
  444.      * file and directory names which the Amiga file system will reject.
  445.      * This is because the Amiga default file system enforces the use
  446.      * of the ISO8559-1 character set. To make a checkout work even with
  447.      * the server supplying PC850 style file names, the following
  448.      * translation may be helpful. Note that there is no counterpart to
  449.      * this in the code that mangles names for transmission to the
  450.      * server. This here is purely for checking out projects which
  451.      * would otherwise be unusable for the Amiga CVS port.
  452.      */
  453.     #ifdef undefined
  454.     {
  455.         static const UBYTE pc850_to_iso8859_1_tab[256] =
  456.         {
  457.             0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
  458.             0x10,0x11,0x12,0x13,0xB6,0xA7,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,
  459.             0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,
  460.             0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,
  461.             0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,
  462.             0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F,
  463.             0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,
  464.             0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F,
  465.             0xC7,0xFC,0xE9,0xE2,0xE4,0xE0,0xE5,0xE7,0xEA,0xEB,0xE8,0xEF,0xEE,0xEC,0xC4,0xC5,
  466.             0xC9,0xE6,0xC6,0xF4,0xF6,0xF2,0xFB,0xF9,0xFF,0xD6,0xDC,0xA2,0xA3,0xA5,0x5F,0x66,
  467.             0xE1,0xED,0xF3,0xFA,0xF1,0xD1,0xAA,0xBA,0xBF,0x5F,0xAC,0xBD,0xBC,0xA1,0xAB,0xBB,
  468.             0x5F,0x7F,0x5F,0x7C,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,
  469.             0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,
  470.             0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,0x5F,
  471.             0x61,0xDF,0x67,0x50,0x53,0x73,0xB5,0x74,0x70,0x54,0x4F,0x64,0x5F,0x66,0x65,0x5F,
  472.             0x5F,0xB1,0x5F,0x5F,0x5F,0x5F,0xF7,0x5F,0xB0,0xB7,0xB7,0x5F,0x6E,0xB2,0x5F,0x20
  473.         };
  474.  
  475.         int c;
  476.  
  477.         get_next_buffer(&buffer);
  478.  
  479.         for(i = 0 ; i < len ; i++)
  480.         {
  481.             c = ((unsigned char *)name)[i];
  482.  
  483.             buffer[i] = (char)pc850_to_iso8859_1_tab[c];
  484.         }
  485.  
  486.         buffer[i] = '\0';
  487.  
  488.         name = buffer;
  489.     }
  490.     #endif
  491.  
  492.     (*name_ptr) = name;
  493.  
  494.     LEAVE();
  495. }
  496.  
  497. /****************************************************************************/
  498.  
  499. void * amiga_valloc(size_t bytes)
  500. {
  501.     void * result;
  502.  
  503.     ENTER();
  504.     SHOWVALUE(bytes);
  505.  
  506.     result = malloc(bytes);
  507.  
  508.     RETURN(result);
  509.     return(result);
  510. }
  511.  
  512. /****************************************************************************/
  513.  
  514. int amiga_symlink(char *to, char *from)
  515. {
  516.     int result;
  517.  
  518.     ENTER();
  519.  
  520.     SHOWSTRING(to);
  521.     SHOWSTRING(from);
  522.  
  523.     result = -1;
  524.     errno = EINVAL;
  525.  
  526.     RETURN(result);
  527.     return(result);
  528. }
  529.  
  530. /****************************************************************************/
  531.  
  532. int amiga_readlink(char *path, char *buf, int buf_size)
  533. {
  534.     int result;
  535.  
  536.     ENTER();
  537.  
  538.     SHOWSTRING(path);
  539.  
  540.     result = -1;
  541.     errno = EINVAL;
  542.  
  543.     RETURN(result);
  544.     return(result);
  545. }
  546.  
  547. /****************************************************************************/
  548.  
  549. unsigned amiga_sleep(unsigned seconds)
  550. {
  551.     Delay(TICKS_PER_SECOND * seconds);
  552.  
  553.     return(0);
  554. }
  555.  
  556. /****************************************************************************/
  557.  
  558. unsigned long amiga_umask(unsigned long mask)
  559. {
  560.     return(0);
  561. }
  562.  
  563. /****************************************************************************/
  564.  
  565. unsigned long amiga_waitpid(unsigned long pid,int *stat_loc,int options)
  566. {
  567.     return(0);
  568. }
  569.  
  570. /****************************************************************************/
  571.  
  572. int amiga_utime(char *name,struct utimbuf *time)
  573. {
  574.     struct DateStamp ds;
  575.     int result = -1;
  576.  
  577.     ENTER();
  578.  
  579.     correct_name(&name);
  580.  
  581.     SHOWSTRING(name);
  582.  
  583.     /* Use the current time? */
  584.     if(time == NULL)
  585.     {
  586.         DateStamp(&ds);
  587.     }
  588.     else
  589.     {
  590.         int minutes_west = amiga_get_minutes_west();
  591.         ULONG seconds;
  592.  
  593.         /* Convert the time given. */
  594.         if(time->modtime < (UNIX_TIME_OFFSET + 60 * minutes_west))
  595.             seconds = 0;
  596.         else
  597.             seconds = time->modtime - (UNIX_TIME_OFFSET + 60 * minutes_west); /* translate from UTC to local time */
  598.  
  599.         ds.ds_Days        = (seconds / (24*60*60));
  600.         ds.ds_Minute    = (seconds % (24*60*60)) / 60;
  601.         ds.ds_Tick        = (seconds % 60) * TICKS_PER_SECOND;
  602.     }
  603.  
  604.     if(SetFileDate((STRPTR)name,&ds))
  605.         result = 0;
  606.  
  607.     RETURN(result);
  608.     return(result);
  609. }
  610.  
  611. /****************************************************************************/
  612.  
  613. int amiga_geteuid(void)
  614. {
  615.     return(0);
  616. }
  617.  
  618. /****************************************************************************/
  619.  
  620. int amiga_getuid(void)
  621. {
  622.     return(0);
  623. }
  624.  
  625. /****************************************************************************/
  626.  
  627. int amiga_getgid(void)
  628. {
  629.     return(0);
  630. }
  631.  
  632. /****************************************************************************/
  633.  
  634. long amiga_getpid(void)
  635. {
  636.     static long old_pid = -1;
  637.     long result;
  638.  
  639.     ENTER();
  640.  
  641.     if(old_pid == -1)
  642.     {
  643.         struct Process * this_process;
  644.         LONG max_cli;
  645.         LONG which;
  646.         LONG i;
  647.  
  648.         this_process = (struct Process *)FindTask(NULL);
  649.  
  650.         Forbid();
  651.  
  652.         which = max_cli = MaxCli();
  653.  
  654.         for(i = 1 ; i <= max_cli ; i++)
  655.         {
  656.             if(FindCliProc(i) == this_process)
  657.             {
  658.                 which = i;
  659.                 break;
  660.             }
  661.         }
  662.  
  663.         Permit();
  664.  
  665.         old_pid = which;
  666.     }
  667.  
  668.     result = old_pid;
  669.  
  670.     RETURN(result);
  671.     return(result);
  672. }
  673.  
  674. /****************************************************************************/
  675.  
  676. char *amiga_getlogin(void)
  677. {
  678.     static char name[256];
  679.     int i;
  680.  
  681.     ENTER();
  682.  
  683.     if(GetVar("USER",name,sizeof(name),0) <= 0)
  684.     {
  685.         if(GetVar("LOGUSER",name,sizeof(name),0) <= 0)
  686.         {
  687.             if(GetVar("USERNAME",name,sizeof(name),0) <= 0)
  688.                 strcpy(name,"anonymous");
  689.         }
  690.     }
  691.  
  692.     for(i = strlen(name)-1 ; i >= 0 ; i--)
  693.     {
  694.         if(name[i] == ' ' || name[i] == '\t' || name[i] == '\r' || name[i] == '\n')
  695.             name[i] = '\0';
  696.         else
  697.             break;
  698.     }
  699.  
  700.     SHOWSTRING(name);
  701.  
  702.     RETURN(name);
  703.     return(name);
  704. }
  705.  
  706. /****************************************************************************/
  707.  
  708. struct passwd *amiga_getpwuid(int uid)
  709. {
  710.     static struct passwd pw;
  711.  
  712.     ENTER();
  713.  
  714.     SHOWVALUE(uid);
  715.  
  716.     memset(&pw,0,sizeof(pw));
  717.  
  718.     pw.pw_dir    = "CVSHOME:";        /* pseudo-home directory */
  719.     pw.pw_gid    = 1;                /* ZZZ wrong */
  720.     pw.pw_name    = amiga_getlogin();
  721.     pw.pw_uid    = uid;                /* ZZZ wrong */
  722.  
  723.     RETURN(&pw);
  724.     return(&pw);
  725. }
  726.  
  727. /****************************************************************************/
  728.  
  729. struct passwd *amiga_getpwnam(char *name)
  730. {
  731.     struct passwd * result;
  732.  
  733.     ENTER();
  734.  
  735.     SHOWSTRING(name);
  736.  
  737.     result = amiga_getpwuid(1);
  738.  
  739.     RETURN(result);
  740.     return(result);
  741. }
  742.  
  743. /****************************************************************************/
  744.  
  745. struct group *amiga_getgrnam(char *name)
  746. {
  747.     struct group * result;
  748.  
  749.     ENTER();
  750.  
  751.     result = NULL;
  752.  
  753.     RETURN(result);
  754.     return(result);
  755. }
  756.  
  757. /****************************************************************************/
  758.  
  759. char *amiga_getpass(char *prompt)
  760. {
  761.     void (*old_sig_handler)(int);
  762.     char * result = NULL;
  763.     BPTR input_stream;
  764.  
  765.     ENTER();
  766.  
  767.     SHOWSTRING(prompt);
  768.  
  769.     /* Let's hope that this really refers to the current input
  770.      * stream...
  771.      */
  772.     input_stream = Input();
  773.  
  774.     old_sig_handler = signal(SIGINT,SIG_IGN);
  775.  
  776.     if(SetMode(input_stream,DOSTRUE))
  777.     {
  778.         static char pwd_buf[128];
  779.         int len,c;
  780.  
  781.         fputs(prompt,stderr);
  782.         fflush(stderr);
  783.  
  784.         len = 0;
  785.         while(TRUE)
  786.         {
  787.             c = -1;
  788.  
  789.             while(TRUE)
  790.             {
  791.                 if(CheckSignal(SIGBREAKF_CTRL_C))
  792.                 {
  793.                     SetMode(input_stream,DOSFALSE);
  794.                     signal(SIGINT,old_sig_handler);
  795.  
  796.                     raise(SIGINT);
  797.  
  798.                     signal(SIGINT,SIG_IGN);
  799.                     SetMode(input_stream,DOSTRUE);
  800.                 }
  801.  
  802.                 if(WaitForChar(input_stream,TICKS_PER_SECOND / 2))
  803.                 {
  804.                     c = fgetc(stdin);
  805.                     if(c == '\003')
  806.                     {
  807.                         SetMode(input_stream,DOSFALSE);
  808.                         signal(SIGINT,old_sig_handler);
  809.  
  810.                         raise(SIGINT);
  811.  
  812.                         signal(SIGINT,SIG_IGN);
  813.                         SetMode(input_stream,DOSTRUE);
  814.                     }
  815.                     else
  816.                     {
  817.                         break;
  818.                     }
  819.                 }
  820.             }
  821.  
  822.             if(c == '\r' || c == '\n')
  823.                 break;
  824.  
  825.             if(((c >= ' ' && c < 127) || (c >= 160)) && len < sizeof(pwd_buf)-1)
  826.             {
  827.                 pwd_buf[len++] = c;
  828.                 pwd_buf[len] = '\0';
  829.             }
  830.         }
  831.  
  832.         SetMode(input_stream,DOSFALSE);
  833.  
  834.         fputs("\n",stderr);
  835.  
  836.         SHOWSTRING(pwd_buf);
  837.  
  838.         result = pwd_buf;
  839.     }
  840.  
  841.     signal(SIGINT,old_sig_handler);
  842.  
  843.     RETURN(result);
  844.     return(result);
  845. }
  846.  
  847. /****************************************************************************/
  848.  
  849. int amiga_gethostname(char * name,int namelen)
  850. {
  851.     static char hostname[256];
  852.     int i,len;
  853.  
  854.     ENTER();
  855.  
  856.     if(GetVar("HOST",hostname,sizeof(hostname),0) <= 0)
  857.     {
  858.         if(GetVar("HOSTNAME",hostname,sizeof(hostname),0) <= 0)
  859.             strcpy(hostname,"anonymous");
  860.     }
  861.  
  862.     for(i = strlen(hostname)-1 ; i >= 0 ; i--)
  863.     {
  864.         if(hostname[i] == ' ' || hostname[i] == '\t' || hostname[i] == '\r' || hostname[i] == '\n')
  865.             hostname[i] = '\0';
  866.         else
  867.             break;
  868.     }
  869.  
  870.     len = strlen(hostname);
  871.     if(len > namelen)
  872.         len = namelen;
  873.  
  874.     memcpy(name,hostname,len);
  875.     name[len] = '\0';
  876.  
  877.     SHOWSTRING(name);
  878.  
  879.     RETURN(0);
  880.     return(0);
  881. }
  882.  
  883. /****************************************************************************/
  884.  
  885. int amiga_pclose(FILE * pipe)
  886. {
  887.     ENTER();
  888.  
  889.     fclose(pipe);
  890.  
  891.     RETURN(0);
  892.     return(0);
  893. }
  894.  
  895. /****************************************************************************/
  896.  
  897. FILE *amiga_popen(char *command, char *mode)
  898. {
  899.     FILE * result = NULL;
  900.     char temp_name[40];
  901.     BPTR output;
  902.  
  903.     ENTER();
  904.  
  905.     correct_name(&command);
  906.  
  907.     SHOWSTRING(command);
  908.     SHOWSTRING(mode);
  909.  
  910.     sprintf(temp_name, "PIPE:%08x.%08lx", (int)FindTask(NULL), time(NULL));
  911.  
  912.     output = Open(temp_name,MODE_NEWFILE);
  913.     if(output != ZERO)
  914.     {
  915.         LONG res;
  916.  
  917.         res = SystemTags(command,
  918.             SYS_Input,        Input(),
  919.             SYS_Output,        output,
  920.             SYS_Asynch,        TRUE,
  921.             SYS_UserShell,    TRUE,
  922.             NP_CloseInput,    FALSE,
  923.         TAG_END);
  924.  
  925.         switch(res)
  926.         {
  927.             case 0:
  928.                 result = fopen(temp_name,mode);
  929.         if(result > 0) amiga_chmod(temp_name, mode);
  930.                 break;
  931.  
  932.             case -1:
  933.                 errno = ENOMEM;
  934.                 Close(output);
  935.                 break;
  936.  
  937.             default:
  938.                 errno = EIO;
  939.                 break;
  940.         }
  941.     }
  942.     else
  943.     {
  944.         errno = EIO;
  945.     }
  946.  
  947.     RETURN(result);
  948.     return(result);
  949. }
  950.  
  951. /****************************************************************************/
  952.  
  953. struct socket_context
  954. {
  955.     int sc_Socket;                            /* The socket involved, or -1 if not used. */
  956.     struct ssh_protocol_context *    sc_SSH;        /* The secure shell data structures involved, * or NULL if not used. */
  957. };
  958.  
  959. /****************************************************************************/
  960.  
  961. static struct socket_context **    socket_table;
  962. static int                        socket_table_size;
  963.  
  964. /****************************************************************************/
  965.  
  966. static struct socket_context *get_registered_socket(int fd)
  967. {
  968.     struct socket_context * result;
  969.  
  970.     if(socket_table != NULL && 0 <= fd && fd < socket_table_size && socket_table[fd] != NULL)
  971.         result = socket_table[fd];
  972.     else
  973.         result = NULL;
  974.  
  975.     return(result);
  976. }
  977.  
  978. static void unregister_socket(int fd)
  979. {
  980.     if(socket_table != NULL && 0 <= fd && fd < socket_table_size && socket_table[fd] != NULL)
  981.     {
  982.         free(socket_table[fd]);
  983.         socket_table[fd] = NULL;
  984.     }
  985. }
  986.  
  987. static struct socket_context *register_socket(int fd)
  988. {
  989.     struct socket_context * result = NULL;
  990.  
  991.     if(socket_table_size <= fd)
  992.     {
  993.         struct socket_context ** new_table;
  994.         int new_table_size;
  995.  
  996.         new_table_size = (fd + 10);
  997.  
  998.         new_table = malloc(sizeof(*new_table) * new_table_size);
  999.         if(new_table == NULL)
  1000.             goto out;
  1001.  
  1002.         if(socket_table != NULL)
  1003.             memcpy(new_table,socket_table,sizeof(*socket_table) * socket_table_size);
  1004.  
  1005.         memset(&new_table[socket_table_size],0,sizeof(*socket_table) * (new_table_size - socket_table_size));
  1006.  
  1007.         free(socket_table);
  1008.  
  1009.         socket_table = new_table;
  1010.         socket_table_size = new_table_size;
  1011.     }
  1012.  
  1013.     socket_table[fd] = malloc(sizeof(*socket_table[fd]));
  1014.     if(socket_table[fd] == NULL)
  1015.         goto out;
  1016.  
  1017.     memset(socket_table[fd],0,sizeof(*socket_table[fd]));
  1018.  
  1019.     result = socket_table[fd];
  1020.  
  1021.   out:
  1022.  
  1023.     return(result);
  1024. }
  1025.  
  1026. /****************************************************************************/
  1027.  
  1028. static void close_libs(void)
  1029. {
  1030.   ENTER();
  1031.  
  1032.     if(LocaleBase != NULL)
  1033.     {
  1034.     #if defined(__MORPHOS__)
  1035.           CloseLibrary(LocaleBase);
  1036.     #else
  1037.       CloseLibrary((struct Library *)LocaleBase);
  1038.         #endif
  1039.  
  1040.     LocaleBase = NULL;
  1041.     }
  1042.  
  1043.     if(SocketBase != NULL)
  1044.     {
  1045.         CloseLibrary(SocketBase);
  1046.         SocketBase = NULL;
  1047.     }
  1048.  
  1049.   LEAVE();
  1050. }
  1051.  
  1052. /****************************************************************************/
  1053.  
  1054. static void initialize_libraries(void)
  1055. {
  1056.   ENTER();
  1057.  
  1058.     if(SocketBase == NULL)
  1059.     {
  1060.         SHOWMSG("opening bsdsocket.library V3");
  1061.  
  1062.         SocketBase = OpenLibrary("bsdsocket.library",3);
  1063.         if(SocketBase != NULL)
  1064.         {
  1065.             extern STRPTR _ProgramName;
  1066.  
  1067.             if(SocketBaseTags(
  1068.                 SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno))),    &errno,
  1069.                 SBTM_SETVAL(SBTC_LOGTAGPTR),                _ProgramName,
  1070.             TAG_END) != 0)
  1071.             {
  1072.                 CloseLibrary(SocketBase);
  1073.                 SocketBase = NULL;
  1074.             }
  1075.         }
  1076.  
  1077.         if(SocketBase == NULL)
  1078.         {
  1079.             fprintf(stderr,"Could not open 'bsdsocket.library' V3; TCP/IP stack not running?\n");
  1080.             exit(RETURN_FAIL);
  1081.         }
  1082.  
  1083.         /* Make sure that the library will eventually be closed. */
  1084.         atexit(close_libs);
  1085.     }
  1086.     else
  1087.     {
  1088.         SHOWMSG("bsdsocket.library already open");
  1089.     }
  1090.  
  1091.   LEAVE();
  1092. }
  1093.  
  1094. /****************************************************************************/
  1095.  
  1096. /* This routine is required because the original abort() may not invoke
  1097.  * the cleanup routines installed by atexit(). Down below we also 'overload'
  1098.  * the runtime library abort() routine to call the following code.
  1099.  */
  1100. void amiga_abort(void)
  1101. {
  1102.     extern STRPTR _ProgramName;
  1103.  
  1104.   ENTER();
  1105.  
  1106.     /* Flush the standard output streams so that
  1107.      * any following output will be printed after
  1108.      * any buffered stdio output.
  1109.      */
  1110.     if(WBenchMsg == NULL)
  1111.     {
  1112.         /* Don't let anybody stop us. */
  1113.         signal(SIGINT,SIG_IGN);
  1114.         signal(SIGTERM,SIG_IGN);
  1115.  
  1116.         fflush(stdout);
  1117.         fflush(stderr);
  1118.     }
  1119.  
  1120.     /* This routine is called when the program is interrupted. */
  1121.     if(((struct Library *)DOSBase)->lib_Version >= 37)
  1122.     {
  1123.         PrintFault(ERROR_BREAK,_ProgramName);
  1124.     }
  1125.     else
  1126.     {
  1127.         const char *famousLastWords = ": *** Break";
  1128.         BPTR output = Output();
  1129.  
  1130.         Write(output,(APTR)famousLastWords,strlen(famousLastWords));
  1131.         Write(output,_ProgramName,strlen(_ProgramName));
  1132.         Write(output,"\n",1);
  1133.     }
  1134.  
  1135.     exit(RETURN_WARN);
  1136. }
  1137.  
  1138. /****************************************************************************/
  1139.  
  1140. void abort(void)
  1141. {
  1142.     amiga_abort();
  1143. }
  1144.  
  1145. /****************************************************************************/
  1146.  
  1147. /* The following routines are really SAS/C specific. */
  1148.  
  1149. #if defined(__SASC)
  1150.  
  1151. void REGARGS __chkabort(void)
  1152. {
  1153.     if(SetSignal(0,0) & SIGBREAKF_CTRL_C)
  1154.         raise(SIGINT);
  1155. }
  1156.  
  1157. void REGARGS _CXBRK(void)
  1158. {
  1159.     abort();
  1160. }
  1161.  
  1162. #endif
  1163.  
  1164. /****************************************************************************/
  1165.  
  1166. char *amiga_strerror(int code)
  1167. {
  1168.     char *result = NULL;
  1169.  
  1170.     ENTER();
  1171.  
  1172.     if(SocketBase != NULL)
  1173.     {
  1174.         struct TagItem tags[2];
  1175.  
  1176.         tags[0].ti_Tag    = SBTM_GETVAL(SBTC_ERRNOSTRPTR);
  1177.         tags[0].ti_Data    = code;
  1178.         tags[1].ti_Tag    = TAG_DONE;
  1179.  
  1180.         if(SocketBaseTagList(tags) == 0)
  1181.             result = (char *)tags[0].ti_Data;
  1182.     }
  1183.  
  1184.     if(result == NULL)
  1185.         result = strerror(code);
  1186.  
  1187.     SHOWSTRING(result);
  1188.  
  1189.     RETURN(result);
  1190.     return(result);
  1191. }
  1192.  
  1193. /****************************************************************************/
  1194.  
  1195. int amiga_mkdir(char *name,int mode)
  1196. {
  1197.     int result = OK;
  1198.     BPTR lock;
  1199.  
  1200.     ENTER();
  1201.  
  1202.     correct_name(&name);
  1203.  
  1204.     SHOWSTRING(name);
  1205.  
  1206.     lock = CreateDir(name);
  1207.     if(lock != ZERO)
  1208.     {
  1209.         UnLock(lock);
  1210.  
  1211.         amiga_chmod(name,mode);
  1212.     }
  1213.     else
  1214.     {
  1215.         LONG error = IoErr();
  1216.  
  1217.         if(error != ERROR_OBJECT_IN_USE)
  1218.         {
  1219.             SetIoErr(error);
  1220.  
  1221.             map_ioerr_to_errno();
  1222.  
  1223.             result = -1;
  1224.         }
  1225.     }
  1226.  
  1227.     RETURN(result);
  1228.     return(result);
  1229. }
  1230.  
  1231. /****************************************************************************/
  1232.  
  1233. struct hostent *amiga_gethostbyname(char *name)
  1234. {
  1235.     struct hostent *result;
  1236.  
  1237.     ENTER();
  1238.     SHOWSTRING(name);
  1239.  
  1240.     initialize_libraries();
  1241.  
  1242.     result = gethostbyname(name);
  1243.  
  1244.     RETURN(result);
  1245.     return(result);
  1246. }
  1247.  
  1248. struct servent *amiga_getservbyname(char *name,char *proto)
  1249. {
  1250.     struct servent *result;
  1251.  
  1252.     ENTER();
  1253.  
  1254.     SHOWSTRING(name);
  1255.     SHOWSTRING(proto);
  1256.  
  1257.     initialize_libraries();
  1258.  
  1259.     result = getservbyname(name,proto);
  1260.  
  1261.     RETURN(result);
  1262.     return(result);
  1263. }
  1264.  
  1265. /****************************************************************************/
  1266.  
  1267. int amiga_bind(int fd,struct sockaddr *name,int namelen)
  1268. {
  1269.     struct socket_context * sc;
  1270.     int result = -1;
  1271.  
  1272.     ENTER();
  1273.  
  1274.     initialize_libraries();
  1275.  
  1276.     sc = get_registered_socket(fd);
  1277.     if(sc == NULL)
  1278.     {
  1279.         errno = EIO;
  1280.         goto out;
  1281.     }
  1282.  
  1283.     if(sc->sc_Socket == -1)
  1284.     {
  1285.         SHOWMSG("not a socket");
  1286.         errno = EBADF;
  1287.         goto out;
  1288.     }
  1289.  
  1290.     result = bind(sc->sc_Socket,name,namelen);
  1291.  
  1292.  out:
  1293.  
  1294.     RETURN(result);
  1295.     return(result);
  1296. }
  1297.  
  1298. /****************************************************************************/
  1299.  
  1300. int amiga_close(int fd)
  1301. {
  1302.     struct socket_context * sc;
  1303.     int result = -1;
  1304.  
  1305.     ENTER();
  1306.  
  1307.     sc = get_registered_socket(fd);
  1308.     if(sc != NULL)
  1309.     {
  1310.         if(sc->sc_Socket != -1)
  1311.         CloseSocket(sc->sc_Socket);
  1312.         else
  1313.             ssh_disconnect(sc->sc_SSH);
  1314.  
  1315.         unregister_socket(fd);
  1316.     }
  1317.  
  1318.     result = close(fd);
  1319.  
  1320.     RETURN(result);
  1321.     return(result);
  1322. }
  1323.  
  1324. /****************************************************************************/
  1325.  
  1326. int amiga_connect(int fd,struct sockaddr *name,int namelen)
  1327. {
  1328.     struct socket_context * sc;
  1329.     int result = -1;
  1330.  
  1331.     ENTER();
  1332.  
  1333.     initialize_libraries();
  1334.  
  1335.     sc = get_registered_socket(fd);
  1336.     if(sc == NULL)
  1337.     {
  1338.         errno = EIO;
  1339.         goto out;
  1340.     }
  1341.  
  1342.     if(sc->sc_Socket == -1)
  1343.     {
  1344.         SHOWMSG("not a socket");
  1345.         errno = EBADF;
  1346.         goto out;
  1347.     }
  1348.  
  1349.     result = connect(sc->sc_Socket,name,namelen);
  1350.  
  1351.  out:
  1352.  
  1353.     RETURN(result);
  1354.     return(result);
  1355. }
  1356.  
  1357. /****************************************************************************/
  1358.  
  1359. int amiga_recv(int fd,void *buff,int nbytes,int flags)
  1360. {
  1361.     struct socket_context * sc;
  1362.     int result = -1;
  1363.  
  1364.     ENTER();
  1365.  
  1366.     initialize_libraries();
  1367.  
  1368.     sc = get_registered_socket(fd);
  1369.     if(sc == NULL)
  1370.     {
  1371.     errno = EIO;
  1372.         goto out;
  1373.     }
  1374.  
  1375.     if(sc->sc_Socket != -1)
  1376.         result = recv(sc->sc_Socket,buff,nbytes,flags);
  1377.     else if (sc->sc_SSH != NULL)
  1378.         result = ssh_read(sc->sc_SSH,buff,nbytes);
  1379.     else
  1380.         errno = EIO;
  1381.  
  1382.  out:
  1383.  
  1384.     RETURN(result);
  1385.     return(result);
  1386. }
  1387.  
  1388. /****************************************************************************/
  1389.  
  1390. int amiga_send(int fd,void *buff,int nbytes,int flags)
  1391. {
  1392.     struct socket_context * sc;
  1393.     int result = -1;
  1394.  
  1395.     ENTER();
  1396.  
  1397.     initialize_libraries();
  1398.  
  1399.     sc = get_registered_socket(fd);
  1400.     if(sc == NULL)
  1401.     {
  1402.         errno = EIO;
  1403.         goto out;
  1404.     }
  1405.  
  1406.     if(sc->sc_Socket != -1)
  1407.         result = send(sc->sc_Socket,buff,nbytes,flags);
  1408.     else if (sc->sc_SSH != NULL)
  1409.         result = ssh_write(sc->sc_SSH,buff,nbytes);
  1410.     else
  1411.         errno = EIO;
  1412.  
  1413.  out:
  1414.  
  1415.     RETURN(result);
  1416.     return(result);
  1417. }
  1418.  
  1419. /****************************************************************************/
  1420.  
  1421. int amiga_shutdown(int fd,int how)
  1422. {
  1423.     struct socket_context * sc;
  1424.     int result = -1;
  1425.  
  1426.     ENTER();
  1427.  
  1428.     initialize_libraries();
  1429.  
  1430.     sc = get_registered_socket(fd);
  1431.     if(sc == NULL)
  1432.     {
  1433.         errno = EIO;
  1434.         goto out;
  1435.     }
  1436.  
  1437.     if(sc->sc_Socket != -1)
  1438.     {
  1439.         result = shutdown(sc->sc_Socket,how);
  1440.     }
  1441.     else if (sc->sc_SSH != NULL && sc->sc_SSH->spc_Socket != -1)
  1442.   {
  1443.     result = shutdown(sc->sc_SSH->spc_Socket, how);
  1444.   }
  1445.   else
  1446.     {
  1447.         SHOWMSG("not a socket");
  1448.         errno = EBADF;
  1449.     }
  1450.  
  1451.  out:
  1452.  
  1453.     RETURN(result);
  1454.     return(result);
  1455. }
  1456.  
  1457. /****************************************************************************/
  1458.  
  1459. int amiga_socket(int domain,int type,int protocol)
  1460. {
  1461.     struct socket_context * sc;
  1462.     int fd;
  1463.  
  1464.     ENTER();
  1465.  
  1466.     initialize_libraries();
  1467.  
  1468.     fd = open("NIL:",O_RDWR,0777);
  1469.     if(fd < 0)
  1470.         goto out;
  1471.  
  1472.     sc = register_socket(fd);
  1473.     if(sc == NULL)
  1474.     {
  1475.         close(fd);
  1476.         fd = -1;
  1477.  
  1478.         errno = ENOMEM;
  1479.         goto out;
  1480.     }
  1481.  
  1482.     sc->sc_Socket = socket(domain,type,protocol);
  1483.     if(sc->sc_Socket < 0)
  1484.     {
  1485.         int error;
  1486.  
  1487.         error = errno;
  1488.  
  1489.         unregister_socket(fd);
  1490.  
  1491.         close(fd);
  1492.         fd = -1;
  1493.  
  1494.         errno = error;
  1495.  
  1496.         goto out;
  1497.     }
  1498.  
  1499.  out:
  1500.  
  1501.     RETURN(fd);
  1502.     return(fd);
  1503. }
  1504.  
  1505. /****************************************************************************/
  1506.  
  1507. int amiga_connect_ssh(char *host_name,char *user_name,char *password,int cipher,int port)
  1508. {
  1509.     struct socket_context * sc;
  1510.     int fd;
  1511.  
  1512.     ENTER();
  1513.  
  1514.     initialize_libraries();
  1515.  
  1516.     fd = open("NIL:",O_RDWR,0777);
  1517.     if(fd < 0)
  1518.         goto out;
  1519.  
  1520.     sc = register_socket(fd);
  1521.     if(sc == NULL)
  1522.     {
  1523.         close(fd);
  1524.         fd = -1;
  1525.  
  1526.         errno = ENOMEM;
  1527.         goto out;
  1528.     }
  1529.  
  1530.     sc->sc_SSH = ssh_connect(host_name,port,user_name,password,cipher);
  1531.     if(sc->sc_SSH == NULL)
  1532.     {
  1533.         unregister_socket(fd);
  1534.  
  1535.         close(fd);
  1536.         fd = -1;
  1537.  
  1538.         errno = EACCES;
  1539.         goto out;
  1540.     }
  1541.  
  1542.     sc->sc_Socket = -1;
  1543.  
  1544.  out:
  1545.  
  1546.     RETURN(fd);
  1547.     return(fd);
  1548. }
  1549.  
  1550. /****************************************************************************/
  1551.  
  1552. int amiga_piped_child(char ** argv,int * to_fd_ptr,int * from_fd_ptr)
  1553. {
  1554.     int len,total_len,quotes,escape,argc,i,j;
  1555.     char * s;
  1556.     char * arg;
  1557.     char * command;
  1558.  
  1559.     BPTR input = ZERO;
  1560.     BPTR output = ZERO;
  1561.     char in_name[40];
  1562.     char out_name[40];
  1563.     int result = -1;
  1564.  
  1565.     ENTER();
  1566.  
  1567.     argc = 0;
  1568.     total_len = 0;
  1569.     for(i = 0 ; argv[i] != NULL ; i++)
  1570.     {
  1571.         argc++;
  1572.         arg = argv[i];
  1573.         len = strlen(arg);
  1574.         quotes = 0;
  1575.  
  1576.         for(j = 0 ; j < len ; j++)
  1577.         {
  1578.             if(arg[j] == ' ' && quotes == 0)
  1579.                 quotes = 2;
  1580.             else if (arg[j] == '\"')
  1581.                 total_len++;
  1582.         }
  1583.  
  1584.         total_len += len + quotes + 1;
  1585.     }
  1586.  
  1587.     command = malloc(total_len+1);
  1588.     if(command == NULL)
  1589.     {
  1590.         errno = ENOMEM;
  1591.         return(-1);
  1592.     }
  1593.  
  1594.     s = command;
  1595.  
  1596.     for(i = 0 ; i < argc ; i++)
  1597.     {
  1598.         arg = argv[i];
  1599.         len = strlen(arg);
  1600.         quotes = escape = 0;
  1601.  
  1602.         for(j = 0 ; j < len ; j++)
  1603.         {
  1604.             if(arg[j] == ' ')
  1605.                 quotes = 1;
  1606.             else if (arg[j] == '\"')
  1607.                 escape = 1;
  1608.  
  1609.             if(quotes && escape)
  1610.                 break;
  1611.         }
  1612.  
  1613.         if(quotes)
  1614.             (*s++) = '\"';
  1615.  
  1616.         for(j = 0 ; j < len ; j++)
  1617.         {
  1618.             if(arg[j] == '\"')
  1619.                 (*s++) = '*';
  1620.  
  1621.             (*s++) = arg[j];
  1622.         }
  1623.  
  1624.         if(quotes)
  1625.             (*s++) = '\"';
  1626.  
  1627.         if(i < argc-1)
  1628.             (*s++) = ' ';
  1629.     }
  1630.  
  1631.     (*s) = '\0';
  1632.  
  1633.     SHOWSTRING(command);
  1634.  
  1635.     sprintf(in_name, "PIPE:in_%08x.%08lx", (int)FindTask(NULL), time(NULL));
  1636.     sprintf(out_name, "PIPE:out_%08x.%08lx", (int)FindTask(NULL), time(NULL));
  1637.  
  1638.     input = Open(in_name,MODE_OLDFILE);
  1639.     output = Open(out_name,MODE_NEWFILE);
  1640.     if(input != ZERO && output != ZERO)
  1641.     {
  1642.         LONG res;
  1643.  
  1644.         res = SystemTags(command,
  1645.             SYS_Input,        input,
  1646.             SYS_Output,        output,
  1647.             SYS_Asynch,        TRUE,
  1648.             SYS_UserShell,    TRUE,
  1649.         TAG_END);
  1650.  
  1651.         switch(res)
  1652.         {
  1653.             case 0:
  1654.                 (*to_fd_ptr) = open(in_name,O_WRONLY,0777);
  1655.                 if((*to_fd_ptr) == -1)
  1656.                     break;
  1657.  
  1658.                 (*from_fd_ptr) = open(out_name,O_RDONLY,0777);
  1659.                 if((*from_fd_ptr) == -1)
  1660.                     break;
  1661.  
  1662.                 result = 0;
  1663.                 break;
  1664.  
  1665.             case -1:
  1666.                 errno = ENOMEM;
  1667.                 Close(input);
  1668.                 Close(output);
  1669.                 break;
  1670.  
  1671.             default:
  1672.                 errno = EIO;
  1673.                 break;
  1674.         }
  1675.     }
  1676.     else
  1677.     {
  1678.         if(input != ZERO)
  1679.             Close(input);
  1680.  
  1681.         if(output != ZERO)
  1682.             Close(output);
  1683.  
  1684.         errno = EIO;
  1685.     }
  1686.  
  1687.     RETURN(result);
  1688.     return(result);
  1689. }
  1690.  
  1691. /****************************************************************************/
  1692.  
  1693. int amiga_isabsolute(char *filename)
  1694. {
  1695.     int result = 0;
  1696.     int i;
  1697.  
  1698.     ENTER();
  1699.  
  1700.     SHOWSTRING(filename);
  1701.  
  1702.     for(i = 0 ; i < strlen(filename) ; i++)
  1703.     {
  1704.         if(filename[i] == ':')
  1705.         {
  1706.             result = 1;
  1707.             break;
  1708.         }
  1709.     }
  1710.  
  1711.     RETURN(result);
  1712.     return(result);
  1713. }
  1714.  
  1715. /****************************************************************************/
  1716.  
  1717. char *amiga_last_component(char *path)
  1718. {
  1719.     char * result;
  1720.  
  1721.     ENTER();
  1722.  
  1723.     SHOWSTRING(path);
  1724.  
  1725.     result = FilePart(path);
  1726.  
  1727.     RETURN(result);
  1728.     return(result);
  1729. }
  1730.  
  1731. /****************************************************************************/
  1732.  
  1733. static int recursive_unlink_file_dir(char *f)
  1734. {
  1735.     D_S(struct FileInfoBlock,fib);
  1736.     BPTR lock;
  1737.     int res = 0;
  1738.  
  1739.     lock = Lock(f,SHARED_LOCK);
  1740.     if(lock != ZERO)
  1741.     {
  1742.         if(Examine(lock,fib))
  1743.         {
  1744.             if(FIB_IS_DRAWER(fib))
  1745.             {
  1746.                 char name[sizeof(fib->fib_FileName)+1];
  1747.  
  1748.                 name[0] = '\0';
  1749.  
  1750.                 while(ExNext(lock,fib))
  1751.                 {
  1752.                     if(SetSignal(0,0) & SIGBREAKF_CTRL_C)
  1753.                     {
  1754.                         res = -1;
  1755.                         break;
  1756.                     }
  1757.  
  1758.                     if(name[0] != '\0')
  1759.                     {
  1760.                         if(DeleteFile(name))
  1761.                         {
  1762.                             res = -1;
  1763.                             break;
  1764.                         }
  1765.  
  1766.                         name[0] = '\0';
  1767.                     }
  1768.  
  1769.                     if(FIB_IS_DRAWER(fib))
  1770.                     {
  1771.                         BPTR old_dir;
  1772.  
  1773.                         old_dir = CurrentDir(lock);
  1774.                         res = recursive_unlink_file_dir(fib->fib_FileName);
  1775.                         CurrentDir(old_dir);
  1776.  
  1777.                         if(res != 0)
  1778.                             break;
  1779.                     }
  1780.  
  1781.                     strcpy(name,fib->fib_FileName);
  1782.                 }
  1783.  
  1784.                 if(res == 0 && name[0] != '\0')
  1785.                 {
  1786.                     if(CANNOT DeleteFile(name))
  1787.                         res = -1;
  1788.                 }
  1789.             }
  1790.  
  1791.             UnLock(lock);
  1792.  
  1793.             if(res == 0)
  1794.             {
  1795.                 if(CANNOT DeleteFile(f))
  1796.                     res = -1;
  1797.             }
  1798.         }
  1799.         else
  1800.         {
  1801.             UnLock(lock);
  1802.         }
  1803.     }
  1804.  
  1805.     return(res);
  1806. }
  1807.  
  1808. int amiga_unlink_file_dir(char * f)
  1809. {
  1810.     int res;
  1811.  
  1812.     ENTER();
  1813.  
  1814.     correct_name(&f);
  1815.  
  1816.     SHOWSTRING(f);
  1817.  
  1818.     res = recursive_unlink_file_dir(f);
  1819.  
  1820.     RETURN(res);
  1821.     return(res);
  1822. }
  1823.  
  1824. /****************************************************************************/
  1825.  
  1826. int amiga_fncmp(char *n1,char *n2)
  1827. {
  1828.     int result;
  1829.  
  1830.     ENTER();
  1831.  
  1832.     SHOWSTRING(n1);
  1833.     SHOWSTRING(n2);
  1834.  
  1835.     result = Stricmp(n1,n2);
  1836.  
  1837.     RETURN(result);
  1838.     return(result);
  1839. }
  1840.  
  1841. /****************************************************************************/
  1842.  
  1843. void amiga_fnfold(char *name)
  1844. {
  1845.     int c;
  1846.  
  1847.     while((c = (*(unsigned char *)name)) != '\0')
  1848.         (*name++) = ToLower(c);
  1849. }
  1850.  
  1851. /****************************************************************************/
  1852.  
  1853. int amiga_fold_fn_char(int c)
  1854. {
  1855.     int result;
  1856.  
  1857.     result = ToLower(c);
  1858.  
  1859.     return(result);
  1860. }
  1861.  
  1862. /****************************************************************************/
  1863.  
  1864. typedef struct name_node
  1865. {
  1866.     struct name_node *    nn_next;
  1867.     char *                nn_name;
  1868.     BOOL                nn_wild;
  1869. } name_node_t;
  1870.  
  1871. /****************************************************************************/
  1872.  
  1873. static int compare(char **a,char **b)
  1874. {
  1875.     return(Stricmp(*a,*b));
  1876. }
  1877.  
  1878. /****************************************************************************/
  1879.  
  1880. void amiga_expand_wild(int argc,char ** argv,int * _argc,char *** _argv)
  1881. {
  1882.     struct AnchorPath * anchor;
  1883.     name_node_t * root;
  1884.     name_node_t * node;
  1885.     name_node_t * next;
  1886.     LONG name_plus;
  1887.     LONG name_total;
  1888.     LONG i;
  1889.  
  1890.     ENTER();
  1891.  
  1892.     anchor        = (struct AnchorPath *)xmalloc(sizeof(*anchor) + 2 * MAX_FILENAME_LEN);
  1893.     root        = NULL;
  1894.     node        = NULL;
  1895.     next        = NULL;
  1896.     name_plus    = 0;
  1897.     name_total    = 0;
  1898.  
  1899.     memset(anchor,0,sizeof(*anchor));
  1900.  
  1901.     anchor->ap_Strlen        = MAX_FILENAME_LEN;
  1902.     anchor->ap_BreakBits    = SIGBREAKF_CTRL_C;
  1903.  
  1904.     for(i = 0 ; i < argc ; i++)
  1905.     {
  1906.         /* Jens: we only check from the start of the real arguments and only expand
  1907.          * wildcards if the previous argument isn`t a option which is identified by a "-"
  1908.          * followed by one character which results in a argv[i-1] of strlen() 2 !!
  1909.      * this will prevent expanding of wildcards in options like "-m" completly.
  1910.          */
  1911.         if(i > 0 && !(*(argv[i-1]) == '-' && strlen(argv[i-1]) == 2) && ParsePatternNoCase(argv[i],anchor->ap_Buf,2 * MAX_FILENAME_LEN) > 0)
  1912.         {
  1913.             LONG result;
  1914.  
  1915.             result = MatchFirst(argv[i],anchor);
  1916.  
  1917.             while(result == 0)
  1918.             {
  1919.                 node = (name_node_t *)malloc(sizeof(*node) + strlen(anchor->ap_Buf) + 1);
  1920.                 if(node == NULL)
  1921.                 {
  1922.                     char buf[80];
  1923.  
  1924.                     MatchEnd(anchor);
  1925.  
  1926.                     sprintf(buf,"out of memory; could not allocate %lu bytes",
  1927.                         (unsigned long)(sizeof(*node) + strlen(anchor->ap_Buf) + 1));
  1928.  
  1929.                     error(1,0,buf);
  1930.                 }
  1931.  
  1932.                 node->nn_name = (char *)(node + 1);
  1933.                 node->nn_next = root;
  1934.                 node->nn_wild = TRUE;
  1935.  
  1936.                 strcpy(node->nn_name,anchor->ap_Buf);
  1937.  
  1938.                 root = node;
  1939.  
  1940.                 name_plus++;
  1941.                 name_total++;
  1942.  
  1943.                 result = MatchNext(anchor);
  1944.             }
  1945.  
  1946.             MatchEnd(anchor);
  1947.         }
  1948.         else
  1949.         {
  1950.             node = (name_node_t *)xmalloc(sizeof(*node));
  1951.  
  1952.             node->nn_name = argv[i];
  1953.             node->nn_next = root;
  1954.             node->nn_wild = FALSE;
  1955.  
  1956.             root = node;
  1957.  
  1958.             name_total++;
  1959.         }
  1960.     }
  1961.  
  1962.     if(name_plus > 0)
  1963.     {
  1964.         char ** last_wild;
  1965.         char ** index;
  1966.  
  1967.         index = (char **)xmalloc(sizeof(char *) * (name_total + 1));
  1968.  
  1969.         (*_argc) = name_total;
  1970.         (*_argv) = index;
  1971.  
  1972.         index = &(index[name_total]);
  1973.  
  1974.         (*index--) = NULL;
  1975.  
  1976.         node            = root;
  1977.         last_wild    = NULL;
  1978.  
  1979.         while(node != NULL)
  1980.         {
  1981.             if(node->nn_wild)
  1982.             {
  1983.                 if(last_wild == NULL)
  1984.                     last_wild = index;
  1985.             }
  1986.             else
  1987.             {
  1988.                 if(last_wild)
  1989.                 {
  1990.                     if((ULONG)last_wild - (ULONG)index > sizeof(char **))
  1991.                         qsort(index + 1,((ULONG)last_wild - (ULONG)index) / sizeof(char **),sizeof(char *), compare);
  1992.  
  1993.                     last_wild = NULL;
  1994.                 }
  1995.             }
  1996.  
  1997.             /* Here we have to call xstrdup() because free_names() will free
  1998.              * that strings later.
  1999.              */
  2000.             (*index--) = xstrdup(node->nn_name);
  2001.  
  2002.             node = node->nn_next;
  2003.         }
  2004.     }
  2005.     else
  2006.     {
  2007.         int i;
  2008.  
  2009.         /* Now we just have to allocate new memory and copy the whole argv[] to this mem
  2010.          * because free_names() is going to free this mem later.
  2011.          */
  2012.         (*_argc) = argc;
  2013.         (*_argv) = (char **) xmalloc(argc * sizeof (char *));
  2014.  
  2015.         for(i = 0; i < argc; ++i)
  2016.             (*_argv)[i] = xstrdup(argv[i]);
  2017.     }
  2018.  
  2019.     /* Now we free that whole list. */
  2020.     node = root;
  2021.  
  2022.     while(node != NULL)
  2023.     {
  2024.         next = node->nn_next;
  2025.         free(node);
  2026.  
  2027.         node = next;
  2028.     }
  2029.  
  2030.     free(anchor);
  2031.  
  2032.     LEAVE();
  2033. }
  2034.  
  2035. /****************************************************************************/
  2036.  
  2037. static void convert_fileinfo_to_stat(struct FileInfoBlock *fib, struct stat *st)
  2038. {
  2039.     ULONG flags;
  2040.     int mode;
  2041.     long time;
  2042.  
  2043.   ENTER();
  2044.  
  2045.     /* This routine converts the contents of a FileInfoBlock
  2046.      * into information to fill a Unix-like stat data structure
  2047.      * with.
  2048.      */
  2049.     flags = fib->fib_Protection ^ (FIBF_READ|FIBF_WRITE|FIBF_EXECUTE|FIBF_DELETE);
  2050.  
  2051.     if(FIB_IS_DRAWER(fib))
  2052.     {
  2053.         /* We always tag directories as available for reading, writing
  2054.          * and searching by the owner.
  2055.          */
  2056.         fib->fib_Protection |= FIBF_READ|FIBF_WRITE|FIBF_EXECUTE|FIBF_DELETE;
  2057.  
  2058.         mode = S_IFDIR;
  2059.     }
  2060.     else
  2061.     {
  2062.         /* Files are always reported as readable by the owner. */
  2063.         fib->fib_Protection |= FIBF_READ;
  2064.  
  2065.         mode = S_IFREG;
  2066.     }
  2067.  
  2068.     if(FLAG_IS_SET(flags,FIBF_READ))
  2069.   {
  2070.         SET_FLAG(mode,S_IRUSR);
  2071.   }
  2072.  
  2073.     if(FLAG_IS_SET(flags,FIBF_WRITE))
  2074.   {
  2075.         SET_FLAG(mode,S_IWUSR);
  2076.   }
  2077.  
  2078.     if(FLAG_IS_SET(flags,FIBF_EXECUTE))
  2079.   {
  2080.       SET_FLAG(mode,S_IXUSR);
  2081.   }
  2082.  
  2083.     if(FLAG_IS_SET(flags,FIBF_GRP_READ))
  2084.     {
  2085.       SET_FLAG(mode,S_IRGRP);
  2086.   }
  2087.  
  2088.     if(FLAG_IS_SET(flags,FIBF_GRP_WRITE))
  2089.   {
  2090.         SET_FLAG(mode,S_IWGRP);
  2091.   }
  2092.  
  2093.     if(FLAG_IS_SET(flags,FIBF_GRP_EXECUTE))
  2094.   {
  2095.         SET_FLAG(mode,S_IXGRP);
  2096.   }
  2097.  
  2098.     if(FLAG_IS_SET(flags,FIBF_OTR_READ))
  2099.   {
  2100.         SET_FLAG(mode,S_IROTH);
  2101.   }
  2102.  
  2103.     if(FLAG_IS_SET(flags,FIBF_OTR_WRITE))
  2104.   {
  2105.         SET_FLAG(mode,S_IWOTH);
  2106.   }
  2107.  
  2108.     if(FLAG_IS_SET(flags,FIBF_OTR_EXECUTE))
  2109.   {
  2110.         SET_FLAG(mode,S_IXOTH);
  2111.   }
  2112.  
  2113.     time = fib->fib_Date.ds_Days * 24*60*60 +
  2114.            fib->fib_Date.ds_Minute * 60 +
  2115.            (fib->fib_Date.ds_Tick / TICKS_PER_SECOND);
  2116.  
  2117.     memset(st, 0, sizeof(struct stat));
  2118.  
  2119.     if(FIB_IS_FILE(fib))
  2120.     {
  2121.         st->st_nlink    = 1;
  2122.         st->st_size        = fib->fib_Size;
  2123.     }
  2124.     else
  2125.     {
  2126.         st->st_nlink = 2;
  2127.     }
  2128.  
  2129.   /* first we fill the struct stat with data we know immediatly */
  2130.   st->st_ino      = fib->fib_DiskKey;
  2131.   st->st_uid      = fib->fib_OwnerUID;
  2132.   st->st_gid      = fib->fib_OwnerGID;
  2133.   st->st_blksize  = S_BLKSIZE;
  2134.   st->st_blocks   = (st->st_size + st->st_blksize-1) / st->st_blksize;
  2135.     st->st_mode          = mode;
  2136.     st->st_mtime      = UNIX_TIME_OFFSET + time + 60 * amiga_get_minutes_west(); /* translate from local time to UTC */
  2137.     st->st_atime      = st->st_mtime;
  2138.     st->st_ctime      = st->st_mtime;
  2139.  
  2140.   LEAVE();
  2141. }
  2142.  
  2143. /****************************************************************************/
  2144.  
  2145. int amiga_stat(char *name,struct stat *st)
  2146. {
  2147.     int result = -1;
  2148.     BPTR fileLock;
  2149.  
  2150.   ENTER();
  2151.  
  2152.   chkabort();
  2153.  
  2154.     correct_name(&name);
  2155.  
  2156.     fileLock = Lock((STRPTR)name,SHARED_LOCK);
  2157.     if(fileLock != ZERO)
  2158.     {
  2159.         D_S(struct FileInfoBlock,fib);
  2160.  
  2161.         if(Examine(fileLock,fib))
  2162.         {
  2163.             BPTR parentDir;
  2164.  
  2165.             /* Check if this is a root directory. */
  2166.             parentDir = ParentDir(fileLock);
  2167.             if(parentDir != ZERO)
  2168.             {
  2169.                 /* This is not the root directory. */
  2170.                 UnLock(parentDir);
  2171.             }
  2172.             else
  2173.             {
  2174.                 /* So this is a root directory. Make sure
  2175.                  * that we return proper protection bits for
  2176.                  * it, i.e. that the directory is always
  2177.                  * readable, writable, etc. This may be
  2178.                  * necessary since on the Amiga, root
  2179.                  * directories cannot have any protection
  2180.                  * bits set. Note that the "deletable"
  2181.                  * bits don't make much sense, but then
  2182.                  * these bits work together with the
  2183.                  * writable bits. The lowest four bits
  2184.                  * remain zero, which enables them all.
  2185.                  */
  2186.                 fib->fib_Protection = FIBF_OTR_READ |
  2187.                                       FIBF_OTR_WRITE |
  2188.                                       FIBF_OTR_EXECUTE |
  2189.                                       FIBF_OTR_DELETE |
  2190.                                       FIBF_GRP_READ |
  2191.                                       FIBF_GRP_WRITE |
  2192.                                       FIBF_GRP_EXECUTE |
  2193.                                       FIBF_GRP_DELETE;
  2194.             }
  2195.  
  2196.             convert_fileinfo_to_stat(fib,st);
  2197.  
  2198.             result = OK;
  2199.         }
  2200.         else
  2201.         {
  2202.             map_ioerr_to_errno();
  2203.         }
  2204.  
  2205.         UnLock(fileLock);
  2206.     }
  2207.     else
  2208.     {
  2209.         map_ioerr_to_errno();
  2210.     }
  2211.  
  2212.   RETURN(result);
  2213.     return(result);
  2214. }
  2215.  
  2216. /****************************************************************************/
  2217.  
  2218. int amiga_lstat(char *name,struct stat *statstruct)
  2219. {
  2220.     int result;
  2221.  
  2222.     result = amiga_stat(name,statstruct);
  2223.  
  2224.     return(result);
  2225. }
  2226.  
  2227. /****************************************************************************/
  2228.  
  2229. int amiga_fstat(int fd,struct stat * st)
  2230. {
  2231.     int result = -1;
  2232.  
  2233.     /* NOTE: SAS/C 6.5x has no fstat() routine in its runtime
  2234.      *       library (this is not required by the ANSI standard).
  2235.      *       So for SAS/C we cook up a version which depends
  2236.      *       upon compiler specific features and assume that for
  2237.      *       everything else, a different solution will be found.
  2238.      */
  2239.     #ifdef __SASC
  2240.     {
  2241.         struct UFB * ufb;
  2242.  
  2243.         chkabort();
  2244.  
  2245.         ufb = chkufb(fd);
  2246.         if(ufb != NULL)
  2247.         {
  2248.             D_S(struct FileInfoBlock,fib);
  2249.  
  2250.             if(ExamineFH(ufb->ufbfh,fib))
  2251.             {
  2252.                 convert_fileinfo_to_stat(fib,st);
  2253.  
  2254.                 result = OK;
  2255.       }
  2256.             else
  2257.             {
  2258.                 map_ioerr_to_errno();
  2259.             }
  2260.         }
  2261.     }
  2262.     #else
  2263.     {
  2264.         result = fstat(fd,st);
  2265.     }
  2266.     #endif /* __SASC */
  2267.  
  2268.     return(result);
  2269. }
  2270.  
  2271. /******************************************************************************/
  2272.  
  2273. int amiga_chmod(char *name,int mode)
  2274. {
  2275.     int result = OK;
  2276.     ULONG flags = 0;
  2277.  
  2278.   ENTER();
  2279.  
  2280.   chkabort();
  2281.  
  2282.     /* Convert the file access modes into
  2283.      * Amiga typical protection bits.
  2284.      */
  2285.     if(FLAG_IS_SET(mode,S_IRUSR))
  2286.   {
  2287.         SET_FLAG(flags,FIBF_READ);
  2288.   }
  2289.  
  2290.     if(FLAG_IS_SET(mode,S_IWUSR))
  2291.     {
  2292.         SET_FLAG(flags,FIBF_WRITE);
  2293.         SET_FLAG(flags,FIBF_DELETE);
  2294.     }
  2295.  
  2296.     if(FLAG_IS_SET(mode,S_IXUSR))
  2297.   {
  2298.         SET_FLAG(flags,FIBF_EXECUTE);
  2299.   }
  2300.  
  2301.     if(FLAG_IS_SET(mode,S_IRGRP))
  2302.     {
  2303.       SET_FLAG(flags,FIBF_GRP_READ);
  2304.   }
  2305.  
  2306.     if(FLAG_IS_SET(mode,S_IWGRP))
  2307.     {
  2308.         SET_FLAG(flags,FIBF_GRP_WRITE);
  2309.         SET_FLAG(flags,FIBF_GRP_DELETE);
  2310.     }
  2311.  
  2312.     if(FLAG_IS_SET(mode,S_IXGRP))
  2313.   {
  2314.         SET_FLAG(flags,FIBF_GRP_EXECUTE);
  2315.   }
  2316.  
  2317.     if(FLAG_IS_SET(mode,S_IROTH))
  2318.   {
  2319.         SET_FLAG(flags,FIBF_OTR_READ);
  2320.   }
  2321.  
  2322.     if(FLAG_IS_SET(mode,S_IWOTH))
  2323.     {
  2324.         SET_FLAG(flags,FIBF_OTR_WRITE);
  2325.         SET_FLAG(flags,FIBF_OTR_DELETE);
  2326.     }
  2327.  
  2328.     if(FLAG_IS_SET(mode,S_IXOTH))
  2329.   {
  2330.         SET_FLAG(flags,FIBF_OTR_EXECUTE);
  2331.   }
  2332.  
  2333.   /* AmigaOS handles the RWED bits different 0 == allowed */
  2334.     flags ^= (FIBF_READ|FIBF_WRITE|FIBF_EXECUTE|FIBF_DELETE);
  2335.  
  2336.     correct_name(&name);
  2337.  
  2338.     if(CANNOT SetProtection(name,flags))
  2339.     {
  2340.         LONG error;
  2341.  
  2342.         error = IoErr();
  2343.         if(error != ERROR_OBJECT_IN_USE)
  2344.         {
  2345.             SetIoErr(error);
  2346.  
  2347.             map_ioerr_to_errno();
  2348.  
  2349.             result = -1;
  2350.         }
  2351.     }
  2352.  
  2353.   RETURN(result);
  2354.     return(result);
  2355. }
  2356.  
  2357. /****************************************************************************/
  2358.  
  2359. int amiga_access(char *name,int modes)
  2360. {
  2361.     int result;
  2362.  
  2363.     ENTER();
  2364.  
  2365.     correct_name(&name);
  2366.  
  2367.     SHOWSTRING(name);
  2368.     SHOWVALUE(modes);
  2369.  
  2370.     /* We ignore the 'x' bit since it doesn't matter
  2371.      * on the Amiga.
  2372.      */
  2373.     result = access(name,modes & ~X_OK);
  2374.  
  2375.     RETURN(result);
  2376.     return(result);
  2377. }
  2378.  
  2379. /****************************************************************************/
  2380.  
  2381. static BPTR home_dir;
  2382.  
  2383. static void restore_home_dir(void)
  2384. {
  2385.     if(home_dir != ZERO)
  2386.     {
  2387.         UnLock(CurrentDir(home_dir));
  2388.         home_dir = ZERO;
  2389.     }
  2390. }
  2391.  
  2392. /****************************************************************************/
  2393.  
  2394. int amiga_chdir(char *path)
  2395. {
  2396.     int result;
  2397.  
  2398.     ENTER();
  2399.  
  2400.     if(home_dir == ZERO)
  2401.     {
  2402.         BPTR old_dir;
  2403.  
  2404.         /* This is tricky at best. chdir() will change the
  2405.          * current directory of this process and unlock the
  2406.          * previously active current directory lock. However,
  2407.          * the current directory lock with which this program
  2408.          * was launched *must not* be unlocked; the same lock
  2409.          * the program was launched with must be the one the
  2410.          * program exits with. This is what we are trying to
  2411.          * achieve here. Note that this strange procedure is
  2412.          * required only because the SAS/C chdir() implementation
  2413.          * will UnLock() the current directory before changing
  2414.          * to the new one.
  2415.          */
  2416.         old_dir = Lock("",SHARED_LOCK);
  2417.         if(old_dir == ZERO)
  2418.         {
  2419.             errno = EIO;
  2420.             result = -1;
  2421.             goto out;
  2422.         }
  2423.  
  2424.         home_dir = CurrentDir(old_dir);
  2425.  
  2426.         atexit(restore_home_dir);
  2427.     }
  2428.  
  2429.     correct_name(&path);
  2430.  
  2431.     SHOWSTRING(path);
  2432.  
  2433.     result = chdir(path);
  2434.  
  2435.  out:
  2436.  
  2437.     RETURN(result);
  2438.     return(result);
  2439. }
  2440.  
  2441. /****************************************************************************/
  2442.  
  2443. int amiga_creat(char *name,int prot)
  2444. {
  2445.     int result;
  2446.  
  2447.     ENTER();
  2448.  
  2449.     correct_name(&name);
  2450.  
  2451.     SHOWSTRING(name);
  2452.     SHOWVALUE(prot);
  2453.  
  2454.     result = creat(name, prot);
  2455.   if(result > -1) amiga_chmod(name, prot);
  2456.  
  2457.     RETURN(result);
  2458.     return(result);
  2459. }
  2460.  
  2461. /****************************************************************************/
  2462.  
  2463. FILE *amiga_fopen(char *name, char *modes)
  2464. {
  2465.     FILE * result;
  2466.  
  2467.     ENTER();
  2468.  
  2469.     correct_name(&name);
  2470.  
  2471.     SHOWSTRING(name);
  2472.     SHOWSTRING(modes);
  2473.  
  2474.     result = fopen(name,modes);
  2475.  
  2476.     RETURN(result);
  2477.     return(result);
  2478. }
  2479.  
  2480. /****************************************************************************/
  2481.  
  2482. int amiga_open(char *name, int flags, ...)
  2483. {
  2484.     int result;
  2485.   int mode;
  2486.   va_list args;
  2487.  
  2488.     ENTER();
  2489.  
  2490.   // lets get the mode if exists
  2491.   va_start(args, flags);
  2492.   mode = va_arg(args, int);
  2493.  
  2494.     correct_name(&name);
  2495.  
  2496.     SHOWSTRING(name);
  2497.     SHOWVALUE(mode);
  2498.  
  2499.     result = open(name, flags);
  2500.   if(result > -1 && FLAG_IS_SET(flags, O_CREAT) && mode) amiga_chmod(name, mode);
  2501.  
  2502.   va_end(args);
  2503.  
  2504.     RETURN(result);
  2505.     return(result);
  2506. }
  2507.  
  2508. /****************************************************************************/
  2509.  
  2510. void *amiga_opendir(char *dir_name)
  2511. {
  2512.     void * result;
  2513.  
  2514.     ENTER();
  2515.  
  2516.     correct_name(&dir_name);
  2517.  
  2518.     SHOWSTRING(dir_name);
  2519.  
  2520.     result = opendir(dir_name);
  2521.  
  2522.     RETURN(result);
  2523.     return(result);
  2524. }
  2525.  
  2526. /****************************************************************************/
  2527.  
  2528. int amiga_rename(char *old,char *new)
  2529. {
  2530.     int result;
  2531.  
  2532.     ENTER();
  2533.  
  2534.     correct_name(&old);
  2535.     correct_name(&new);
  2536.  
  2537.     SHOWSTRING(old);
  2538.     SHOWSTRING(new);
  2539.  
  2540.     /* NOTE: Unix-style rename, which will cause a file of the same
  2541.      *       name to be removed first.
  2542.      */
  2543.  
  2544.     result = rename(old,new);
  2545.     if(result == -1 && errno == EEXIST)
  2546.     {
  2547.         if(CANNOT DeleteFile(new))
  2548.         {
  2549.             LONG error = IoErr();
  2550.  
  2551.             if(error == ERROR_DELETE_PROTECTED)
  2552.             {
  2553.                 SetProtection(new,0);
  2554.                 DeleteFile(new);
  2555.             }
  2556.         }
  2557.  
  2558.         result = rename(old,new);
  2559.     }
  2560.  
  2561.     RETURN(result);
  2562.     return(result);
  2563. }
  2564.  
  2565. /****************************************************************************/
  2566.  
  2567. int amiga_rmdir(char *name)
  2568. {
  2569.     int result;
  2570.  
  2571.     ENTER();
  2572.  
  2573.     correct_name(&name);
  2574.  
  2575.     SHOWSTRING(name);
  2576.  
  2577.     result = rmdir(name);
  2578.  
  2579.     RETURN(result);
  2580.     return(result);
  2581. }
  2582.  
  2583. /****************************************************************************/
  2584.  
  2585. int amiga_unlink(char *name)
  2586. {
  2587.     int result = 0;
  2588.  
  2589.     ENTER();
  2590.  
  2591.     correct_name(&name);
  2592.  
  2593.     SHOWSTRING(name);
  2594.  
  2595.     if(CANNOT DeleteFile(name))
  2596.     {
  2597.         LONG error;
  2598.  
  2599.         /* This is tricky; CVS can be run either by the administrator
  2600.          * or by a humble user. Trouble is, there is no such distinction
  2601.          * on the Amiga, which effectively means that the user is also
  2602.          * the administrator. In this particular case it means that the
  2603.          * file to be removed must be removed even if it is protected
  2604.          * from deletion. Why is this important? If we don't do this,
  2605.          * then checking out CVSROOT files and modifying them (such as
  2606.          * the modules file) will not work.
  2607.          */
  2608.         error = IoErr();
  2609.         if(error == ERROR_DELETE_PROTECTED)
  2610.         {
  2611.             if(SetProtection(name,0) && DeleteFile(name))
  2612.                 error = 0;
  2613.             else
  2614.                 error = IoErr();
  2615.         }
  2616.  
  2617.         if(error != 0)
  2618.         {
  2619.             SetIoErr(error);
  2620.  
  2621.             map_ioerr_to_errno();
  2622.  
  2623.             result = -1;
  2624.         }
  2625.     }
  2626.  
  2627.     RETURN(result);
  2628.     return(result);
  2629. }
  2630.  
  2631. /****************************************************************************/
  2632.  
  2633. unsigned char *amiga_inet_ntoa(struct in_addr iaddr)
  2634. {
  2635.     return(Inet_NtoA(iaddr.s_addr));
  2636. }
  2637.  
  2638. /****************************************************************************/
  2639.  
  2640. FILE *amiga_cvs_temp_file(char **filename)
  2641. {
  2642.     FILE *fp = NULL;
  2643.     char *fn;
  2644.  
  2645.     fn = amiga_cvs_temp_name();
  2646.     if(fn != NULL)
  2647.     {
  2648.         fp = amiga_fopen(fn,"w+");
  2649.         if(fp != NULL);
  2650.             amiga_chmod(fn,0600);
  2651.     }
  2652.  
  2653.     (*filename) = fn;
  2654.  
  2655.     return(fp);
  2656. }
  2657.  
  2658. /****************************************************************************/
  2659.  
  2660. char *amiga_cvs_temp_name(void)
  2661. {
  2662.     extern char *Tmpdir;
  2663.     char *value;
  2664.     char *retval;
  2665.     int max_len;
  2666.     int len;
  2667.  
  2668.     ENTER();
  2669.  
  2670.     max_len = strlen(Tmpdir) + 40;
  2671.  
  2672.     value = xmalloc(max_len);
  2673.  
  2674.     strcpy(value,Tmpdir);
  2675.     len = strlen(value);
  2676.     while(len > 0 && value[len-1] == '/')
  2677.         value[--len] = '\0';
  2678.  
  2679.     AddPart(value,"cvsXXXXXX",max_len);
  2680.  
  2681.     retval = mktemp(value);
  2682.  
  2683.     if(retval == NULL)
  2684.         error(1,errno,"could not generate temporary filename");
  2685.  
  2686.     SHOWSTRING(value);
  2687.  
  2688.     RETURN(value);
  2689.     return(value);
  2690. }
  2691.  
  2692. /****************************************************************************/
  2693.  
  2694. static int amiga_rcmd(char **remote_hostname,int remote_port,char *local_user,char *remote_user,char *command)
  2695. {
  2696.     struct hostent *remote_hp;
  2697.     struct hostent *local_hp;
  2698.     struct sockaddr_in remote_isa;
  2699.     struct sockaddr_in local_isa;
  2700.     char local_hostname[80];
  2701.     char ch;
  2702.     int s;
  2703.     int local_port;
  2704.     int rs;
  2705.  
  2706.     remote_hp = amiga_gethostbyname(*remote_hostname);
  2707.     if(remote_hp == NULL)
  2708.     {
  2709.         fprintf(stderr,"Could not obtain address of remote host '%s' (%d, %s).\n",(*remote_hostname),errno,amiga_strerror(errno));
  2710.         exit(1);
  2711.     }
  2712.  
  2713.     /* Copy remote IP address into socket address structure */
  2714.     memset(&remote_isa,0,sizeof(remote_isa));
  2715.     remote_isa.sin_family = AF_INET;
  2716.     remote_isa.sin_port = htons(remote_port);
  2717.     memcpy(&remote_isa.sin_addr,remote_hp->h_addr,sizeof(remote_isa.sin_addr));
  2718.  
  2719.     amiga_gethostname(local_hostname,sizeof(local_hostname));
  2720.     local_hp = amiga_gethostbyname(local_hostname);
  2721.     if(local_hp == NULL)
  2722.     {
  2723.         fprintf(stderr,"Could not obtain local host address (%d, %s).\n",errno,amiga_strerror(errno));
  2724.         exit(1);
  2725.     }
  2726.  
  2727.     /* Copy local IP address into socket address structure */
  2728.     memset(&local_isa,0,sizeof(local_isa));
  2729.     local_isa.sin_family = AF_INET;
  2730.     memcpy(&local_isa.sin_addr,local_hp->h_addr,sizeof(local_isa.sin_addr));
  2731.  
  2732.     /* Create the local socket */
  2733.     s = amiga_socket(AF_INET,SOCK_STREAM,0);
  2734.     if(s < 0)
  2735.     {
  2736.         fprintf(stderr,"Socket creation failed (%d, %s).\n",errno,amiga_strerror(errno));
  2737.         exit(1);
  2738.     }
  2739.  
  2740.     /* Bind local socket with a port from IPPORT_RESERVED/2 to IPPORT_RESERVED - 1
  2741.      * this requires the OPER privilege under VMS -- to allow communication with
  2742.      * a stock rshd under UNIX
  2743.      */
  2744.     rs = 0;
  2745.     for(local_port = IPPORT_RESERVED - 1; local_port >= IPPORT_RESERVED/2; local_port--)
  2746.     {
  2747.         local_isa.sin_port = htons(local_port);
  2748.         rs = amiga_bind(s,(struct sockaddr *)&local_isa,sizeof(local_isa));
  2749.         if(rs == 0)
  2750.             break;
  2751.     }
  2752.  
  2753.     /* Bind local socket to an unprivileged port.  A normal rshd will drop the
  2754.      * connection; you must be running a patched rshd invoked through inetd for
  2755.      * this connection method to work
  2756.      */
  2757.  
  2758.     if(rs != 0)
  2759.     {
  2760.         for(local_port = IPPORT_USERRESERVED - 1;
  2761.             local_port > IPPORT_RESERVED;
  2762.             local_port--)
  2763.         {
  2764.             local_isa.sin_port = htons(local_port);
  2765.             rs = amiga_bind(s,(struct sockaddr *)&local_isa,sizeof(local_isa));
  2766.             if(rs == 0)
  2767.                 break;
  2768.         }
  2769.     }
  2770.  
  2771.     rs = amiga_connect(s,(struct sockaddr *) &remote_isa,sizeof(remote_isa));
  2772.     if(rs == -1)
  2773.     {
  2774.         fprintf(stderr,"Could not connect to %s:%d (%d, %s).\n",(*remote_hostname),remote_port,errno,amiga_strerror(errno));
  2775.         amiga_close(s);
  2776.         exit(2);
  2777.     }
  2778.  
  2779.     /* Now supply authentication information */
  2780.  
  2781.     /* Auxiliary port number for error messages, we don't use it */
  2782.     amiga_send(s,"0\0",2,0);
  2783.  
  2784.     /* Who are we */
  2785.     amiga_send(s,local_user,strlen(local_user) + 1,0);
  2786.  
  2787.     /* Who do we want to be */
  2788.     amiga_send(s,remote_user,strlen(remote_user) + 1,0);
  2789.  
  2790.     /* What do we want to run */
  2791.     amiga_send(s,command,strlen(command) + 1,0);
  2792.  
  2793.     /* NUL is sent back to us if information is acceptable */
  2794.     if(amiga_recv(s,&ch,1,0) != 1)
  2795.         return(-1);
  2796.  
  2797.     if(ch != '\0')
  2798.     {
  2799.         errno = EPERM;
  2800.         return -1;
  2801.     }
  2802.  
  2803.     return s;
  2804. }
  2805.  
  2806. /****************************************************************************/
  2807.  
  2808. static char *cvs_server;
  2809. static char *command;
  2810.  
  2811. extern int trace;
  2812.  
  2813. void amiga_start_server(int *tofd,int *fromfd,char *client_user,char *server_user,char *server_host,char *server_cvsroot)
  2814. {
  2815.     int fd,port;
  2816.     char *portenv;
  2817.     struct servent *sptr;
  2818.     char * shell_name;
  2819.  
  2820.     shell_name = amiga_getenv("CVS_RSH");
  2821.     if(shell_name == NULL)
  2822.         shell_name = "rsh";
  2823.  
  2824.     cvs_server = amiga_getenv("CVS_SERVER");
  2825.     if(cvs_server == NULL)
  2826.         cvs_server = "cvs";
  2827.  
  2828.     command = xmalloc(strlen(cvs_server)
  2829.                       + strlen(server_cvsroot)
  2830.                       + 50);
  2831.     sprintf(command,"%s server",cvs_server);
  2832.  
  2833.     portenv = amiga_getenv("CVS_RCMD_PORT");
  2834.     if(portenv != NULL)
  2835.     {
  2836.         port = atoi(portenv);
  2837.     }
  2838.     else
  2839.     {
  2840.         sptr = amiga_getservbyname("shell","tcp");
  2841.         if(sptr != NULL)
  2842.             port = sptr->s_port;
  2843.         else
  2844.             port = 514; /* shell/tcp */
  2845.     }
  2846.  
  2847.     if(trace)
  2848.     {
  2849.         fprintf(stderr,"amiga_start_server(): connecting to %s:%d\n",
  2850.                 server_host,port);
  2851.  
  2852.         fprintf(stderr,"local_user = %s, remote_user = %s, CVSROOT = %s\n",
  2853.                 client_user,(server_user ? server_user : client_user),
  2854.                 server_cvsroot);
  2855.     }
  2856.  
  2857.     if(strcmp(shell_name,"ssh") == SAME || strcmp(shell_name,"ssh1") == SAME)
  2858.     {
  2859.         char *password = NULL;
  2860.         char *ssh_passfile;
  2861.         char *cvsrootstr;
  2862.         BOOL password_found_in_file = FALSE;
  2863.  
  2864.         /* Allocate local memory for the larger buffers. */
  2865.         cvsrootstr = xmalloc(1024);
  2866.  
  2867.         /* Now we check if the special CVS_SSH_PASSFILE variable is enabled.
  2868.          * Please note that it is ABSOLUTLY INSECURE to use this PASSFILE option.
  2869.          * It was added on user request, but we do not recommend to use it.
  2870.          */
  2871.         ssh_passfile = amiga_getenv("CVS_SSH_PASSFILE");
  2872.         if(ssh_passfile != NULL)
  2873.         {
  2874.             FILE *fh;
  2875.  
  2876.             sprintf(cvsrootstr,":server:%s@%s:%s ",server_user,server_host,server_cvsroot);
  2877.  
  2878.             /* Now we check if an entry exists in the passfile. */
  2879.             fh = amiga_fopen(ssh_passfile,"r");
  2880.             if(fh != NULL)
  2881.             {
  2882.                 int line_length;
  2883.                 char *linebuf = NULL;
  2884.                 size_t linebuf_len;
  2885.  
  2886.                 while(password == NULL && (line_length = getline(&linebuf,&linebuf_len,fh)) >= 0)
  2887.                 {
  2888.                     /* Now we remove the finishing line feed. */
  2889.                     while(line_length > 0 && linebuf[line_length-1] == '\n')
  2890.                         linebuf[--line_length] = '\0';
  2891.  
  2892.                     if(strncmp(linebuf,cvsrootstr,strlen(cvsrootstr)) == SAME)
  2893.                     {
  2894.                         char *passphrase = linebuf+strlen(cvsrootstr);
  2895.  
  2896.                         if(passphrase[0] != 'A')
  2897.                             error(1,0,"corrupt SSH passfile entry.");
  2898.  
  2899.                         password = descramble(passphrase);
  2900.                         password_found_in_file = TRUE;
  2901.                     }
  2902.                 }
  2903.                 fclose(fh);
  2904.             }
  2905.         }
  2906.  
  2907.         if(password == NULL)
  2908.         {
  2909.             char *prompt;
  2910.  
  2911.             prompt = xmalloc(400);
  2912.  
  2913.             sprintf(prompt,"Password for %s@%s: ",(server_user ? server_user : client_user),server_host);
  2914.  
  2915.             password = amiga_getpass(prompt);
  2916.  
  2917.             free(prompt);
  2918.         }
  2919.  
  2920.         if(password != NULL)
  2921.         {
  2922.             int cipher = SSH_CIPHER_3DES;
  2923.             int port = SSH_PORT;
  2924.             char * cipher_name;
  2925.             char * port_number;
  2926.  
  2927.             cipher_name = amiga_getenv("CVS_SSH_CIPHER");
  2928.             if(cipher_name != NULL && stricmp(cipher_name,"blowfish") == SAME)
  2929.                 cipher = SSH_CIPHER_BLOWFISH;
  2930.  
  2931.             port_number = amiga_getenv("CVS_SSH_PORT");
  2932.             if(port_number != NULL)
  2933.             {
  2934.                 int n;
  2935.  
  2936.                 n = atoi(port_number);
  2937.                 if(1 <= n && n < 32768)
  2938.                     port = n;
  2939.             }
  2940.  
  2941.             fd = amiga_connect_ssh(server_host,(server_user != NULL) ? server_user : client_user,password,cipher,port);
  2942.             if(fd != -1)
  2943.             {
  2944.                 struct socket_context * sc;
  2945.  
  2946.                 sc = get_registered_socket(fd);
  2947.                 if(sc != NULL)
  2948.                 {
  2949.                     /* We can now save the password in the passfile
  2950.                      * unless it already exists.
  2951.                      */
  2952.                     if(ssh_passfile != NULL && NOT password_found_in_file)
  2953.                     {
  2954.                         FILE *fh;
  2955.  
  2956.                         /* Check if a entry exists in the passfile. */
  2957.                         fh = amiga_fopen(ssh_passfile,"a");
  2958.                         if(fh != NULL)
  2959.                         {
  2960.                             if(fprintf(fh,"%s%s\n",cvsrootstr,scramble(password)) < 0)
  2961.                                 error(1,errno,"could not write to '%s'",ssh_passfile);
  2962.  
  2963.                             fclose(fh);
  2964.                         }
  2965.                         else
  2966.                         {
  2967.                             error(1,errno,"could not open '%s' for writing",ssh_passfile);
  2968.                         }
  2969.                     }
  2970.  
  2971.                     if(ssh_execute_cmd(sc->sc_SSH,command) == -1)
  2972.                     {
  2973.                         amiga_close(fd);
  2974.                         fd = -1;
  2975.                     }
  2976.                 }
  2977.                 else
  2978.                 {
  2979.                     amiga_close(fd);
  2980.                     fd = -1;
  2981.                 }
  2982.             }
  2983.         }
  2984.         else
  2985.         {
  2986.             fd = -1;
  2987.         }
  2988.  
  2989.         free(cvsrootstr);
  2990.     }
  2991.     else
  2992.     {
  2993.         fd = amiga_rcmd(&server_host,port,
  2994.                         client_user,
  2995.                         (server_user ? server_user : client_user),
  2996.                         command);
  2997.     }
  2998.  
  2999.     if(fd < 0)
  3000.         error(1,errno,"could not start server via rcmd()");
  3001.  
  3002.     (*tofd) = fd;
  3003.     (*fromfd) = fd;
  3004.  
  3005.     free(command);
  3006. }
  3007.  
  3008. /****************************************************************************/
  3009.  
  3010. void amiga_shutdown_server_input(int fd)
  3011. {
  3012.     ENTER();
  3013.  
  3014.   // make sure write&read channels are shutdown
  3015.     if(amiga_shutdown(fd, 2) < 0 && errno != ENOTSOCK)
  3016.         error(1,0,"could not shutdown() input server connection");
  3017.  
  3018.   // then free this thing
  3019.     if(amiga_close(fd) < 0)
  3020.         error(1,0,"could not close() server connection");
  3021.  
  3022.     LEAVE();
  3023. }
  3024.  
  3025. /****************************************************************************/
  3026.  
  3027. void amiga_shutdown_server_output(int fd)
  3028. {
  3029.     ENTER();
  3030.  
  3031.   // make sure write&read channels are shutdown
  3032.     if(amiga_shutdown(fd, 1) < 0 && errno != ENOTSOCK)
  3033.         error(1,0,"could not shutdown() output server connection");
  3034.  
  3035.     LEAVE();
  3036. }
  3037.  
  3038. /****************************************************************************/
  3039.  
  3040. void amiga_system_initialize(int * _argc, char *** _argv)
  3041. {
  3042. /* We have to take care that the 68k libnix doesn`t support
  3043.    some of the stuff SAS/C supports by default and also we have to
  3044.    take care that the morphOS libnix supports it
  3045. */
  3046. #if defined(__GNUC__) && !defined(__MORPHOS__)
  3047.   if(WBenchMsg != NULL)
  3048.   {
  3049.     _ProgramName = (char *)WBenchMsg->sm_ArgList[0].wa_Name;
  3050.   }
  3051.   else
  3052.   {
  3053.     _ProgramName = (char *)*_argv[0];
  3054.   }
  3055. #endif
  3056.  
  3057.     amiga_expand_wild((*_argc),(*_argv),_argc,_argv);
  3058. }
  3059.  
  3060. /****************************************************************************/
  3061.  
  3062. char *amiga_getenv(char * name)
  3063. {
  3064.     static char local_buffer[512];
  3065.     char * result = NULL;
  3066.  
  3067.     if(GetVar((STRPTR)name,local_buffer,sizeof(local_buffer),0) > 0)
  3068.     {
  3069.         result = malloc(strlen(local_buffer)+1);
  3070.         if(result != NULL)
  3071.             strcpy(result,local_buffer);
  3072.     }
  3073.  
  3074.     return(result);
  3075. }
  3076.  
  3077. /****************************************************************************/
  3078.  
  3079. /* We have to provide our own functions for several things because
  3080.    the libnix version of morphos doesn`t provide those functions yet.
  3081.    But as soon as it is supported we can remove those functions here.
  3082. */
  3083. #if defined(__MORPHOS__)
  3084.  
  3085. #include <unistd.h>
  3086. #include <proto/exec.h>
  3087.  
  3088. char *mktemp(char *buf)
  3089. {
  3090.   long pid = (long)FindTask(0L);
  3091.   char *c = buf;
  3092.  
  3093.   while(*c++); --c;
  3094.  
  3095.   while(*--c == 'X')
  3096.   {
  3097.     *c = pid % 10 + '0';
  3098.     pid /= 10;
  3099.   }
  3100.  
  3101.   if (++c,*c)
  3102.   {
  3103.     for(*c='A'; *c <= 'Z'; (*c)++)
  3104.     {
  3105.       if (access(buf,0))
  3106.       {
  3107.         return buf;
  3108.       }
  3109.     }
  3110.     *c = 0;
  3111.   }
  3112.  
  3113.   return buf;
  3114. }
  3115.  
  3116. #include <fcntl.h>
  3117.  
  3118. int creat(const char *path, mode_t mode)
  3119. {
  3120.   return open(path, O_CREAT | O_TRUNC | O_WRONLY, mode);
  3121. }
  3122.  
  3123. #include <unistd.h>
  3124.  
  3125. int rmdir(const char *pathname)
  3126. {
  3127.   return remove(pathname);
  3128. }
  3129.  
  3130. #endif /* __MORPHOS__ */
  3131.